package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.dto.LoginDTO;
import com.example.springboot.controller.requests.AdminPageRequest;
import com.example.springboot.controller.requests.LoginRequest;
import com.example.springboot.controller.requests.UserPageRequst;
import com.example.springboot.controller.requests.changePassRequest;
import com.example.springboot.entity.Admin;
import com.example.springboot.service.IAdminService;
import com.example.springboot.service.IUserService;
import com.example.springboot.service.impl.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    IAdminService userService;
    @GetMapping("/list")
    public Result list() {
        List<Admin> users=userService.list();
        return  Result.success(users);
    }
    @PostMapping("/save")
    public  Result save( @RequestBody Admin user)
    {
        userService.save(user);
        return Result.success();
    }

    @GetMapping("/page")
    public Result page(AdminPageRequest userPageRequst)
    {
        return  Result.success(userService.page(userPageRequst));
    }
    @GetMapping("/{id}")
    public Result getByid(@PathVariable Integer id)
    {
         Admin users=userService.getByid(id);
         System.out.println(users);
        return Result.success(users);
    }
    @PutMapping("/update")
    public  Result update( @RequestBody Admin user)
    {
        userService.update(user);
        return Result.success();
    }
    @PutMapping("/password")
    public Result password(@RequestBody changePassRequest changePassRequest)
    {
        if(changePassRequest.getID()>0 &&changePassRequest.getNewpass()!=null)
        {
            userService.savepass(changePassRequest);
            return Result.success();
        }
        else
            return Result.erro("修改密码不合理或用户不存在");

    }
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public Result delete(@PathVariable Integer id)
    {
        userService.delete(id);
        return Result.success();
    }
@PostMapping ("/login")
public  Result login( @RequestBody LoginRequest request)
{
//
//    LoginRequest request=new LoginRequest();
    LoginDTO admin= userService.login(request);

    return Result.success(admin);
}
}
